package com.zian.handler;

import com.zian.common.R;
import com.zian.exception.CommonException;
import com.zian.exception.DbException;
import com.zian.exception.LoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import org.springframework.web.util.NestedServletException;

import java.util.stream.Collectors;

@RestControllerAdvice
@Slf4j
public class CommonExceptionAdvice {


    @ExceptionHandler(DbException.class)
    public R handleDbException(DbException e) {
        log.error("数据库操作异常 -> ", e);
        return R.error("系统异常");
    }

    @ExceptionHandler(LoginException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public R handleLoginException(LoginException e) {
        log.error("登录异常 -> {}", e.getMessage());
        log.debug("", e);
        return R.error(401,e.getMessage());
    }

    @ExceptionHandler(CommonException.class)
    public R handleBadRequestException(CommonException e) {
        log.error("自定义异常 -> {} , 异常原因：{}  ", e.getClass().getName(), e.getMessage());
        log.debug("", e);
        return R.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String msg = e.getBindingResult().getAllErrors()
                .stream().map(ObjectError::getDefaultMessage)
                .collect(Collectors.joining("|"));
        log.error("请求参数校验异常 -> {}", msg);
        log.debug("", e);
        return R.error(msg);
    }

    @ExceptionHandler(BindException.class)
    public R handleBindException(BindException e) {
        log.error("请求参数绑定异常 ->BindException， {}", e.getMessage());
        log.debug("", e);
        return R.error("请求参数格式错误");
    }

    @ExceptionHandler(NestedServletException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handleNestedServletException(NestedServletException e) {
        log.error("参数异常 -> NestedServletException，{}", e.getMessage());
        log.debug("", e);
        return R.error("请求参数处理异常");
    }

    /**
     * 处理找不到的资源 请求：由于 SpringSecurity 中 404 错误会被 SpringSecurity 拦截，所以这里需要单独处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("请求方式不允许 -> HttpRequestMethodNotSupportedException，{}", e.getMessage());
        log.debug("", e);
        return R.error(405, "请求方式不允许");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("请求参数格式错误 -> HttpMessageNotReadableException，{}", e.getMessage());
        log.debug("", e);
        return R.error("请求参数格式错误");
    }


    @ExceptionHandler(Exception.class)
    public R handleRuntimeException(Exception e) {
        log.error("其他异常 uri : {} -> ", e.getMessage(), e);
        return R.error("服务器异常");
    }
}
